package cn.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.junit.Test;

import cn.jsj.hzu.qm.facecloud.pyAi.LACConnecter;
import cn.jsj.hzu.qm.facecloud.pyAi.LACObject;


public class test {
	@Test
	public void name() throws IOException {
		String my_str = "666";
		FileOutputStream out = new FileOutputStream(new File("D:\\out.txt"));
		//1 新建对象
		LACConnecter lacConnecter = new LACConnecter();
		
		//2 获取链接 
		LACObject lacObject = lacConnecter.getConnectionLacObject();
		if (lacObject == null) {
			out.write("服务器忙<br>server is busy".getBytes());
			return;
		}
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//读照片
		FileInputStream file = new FileInputStream(new File("D:\\1_my1.jpg"));
		List<Integer> list = new LinkedList<Integer>();
		int b = 0;
		while((b = file.read())!= -1) {
			list.add(b);
		}
		
		//3 设置文本
		lacObject.setInJpgImg(list);
		
		//4 启动
		int i = lacObject.setStateToRun();
		if (i == -1) {
			out.write("Error in start".getBytes());
			return;
		}
		
		//5 等待并获取结果
		
		long sleep = 0;
		boolean isGood = false;
		while (sleep <= 5000) {
			if (lacObject.getState().equals(LACObject.STATE_OUT_READY) == true) {
				out.write(lacObject.getOutString().getBytes());
				isGood = true;
				break;
			}
			
			try {
				sleep += 300;
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(isGood == false) {
			out.write("OVER TIME-服务器内部响应超时".getBytes());
		}
		
		//6 释放
		lacConnecter.free(lacObject);
	}
}
